<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="4">
        <avue-tree
            :option="treeOption"
            :data="treeData"
            ref="tree"
            @node-click="deptClick"
        ></avue-tree>
      </el-col>
      <el-col :span="20" style="height: calc(100% - 60px)">
        <avue-crud
            :option="option"
            :data="data"
            :table-loading="option.loading"
            :permission="permissionList"
            v-model:page="page"
            @row-save="save"
            @row-del="del"
            @row-update="update"
            @on-load="init"
            @refresh-change="refresh"
            @search-change="search"
            @selection-change="selectChange"
            v-model="form"
        >
          <template #sex="scope">
            <el-tag v-if="scope.row.sex===1"> {{ scope.row.$sex }}</el-tag>
            <el-tag v-if="scope.row.sex===2" type="success"> {{ scope.row.$sex }}</el-tag>
            <el-tag v-if="scope.row.sex===0" type="danger"> {{ scope.row.$sex }}</el-tag>
          </template>
          <template #menu-left="{ size }">
            <el-button
                type="danger"
                :size="size"
                icon="el-icon-delete"
                v-if="!this.validatenull(this.permission.SysUser_remove)"
                @click="delBatch"
            >批量删除
            </el-button>
            <el-button
                type="success"
                :size="size"
                icon="el-icon-refresh"
                v-if="!this.validatenull(this.permission.SysUser_resetPassword)"
                @click="resetPassword"
            >重置密码
            </el-button>
            <el-button
                type="success"
                :size="size"
                icon="el-icon-refresh"
                @click="download"
            >导 出
            </el-button>
          </template>
        </avue-crud>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {
  list,
  pageList,
  query,
  save,
  update,
  remove,
  resetPassword,
} from "@/api/admin/SysUserApi.js";
import {treeList} from "@/api/admin/SysDeptApi.js";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      data: [],
      form: {},
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectDeptId: null,
      selectionList: [],
      params: {},
      treeData: [],
      treeOption: {
        defaultExpandAll: true,
        props: {
          label: "name",
          value: "id",
        },
      },
      option: {
        loading: true,
        selection: true,
        searchMenuSpan: 4,
        menuWidth: 400,
        height: "auto",
        calcHeight: "11",
        stripe: true,
        border: true,
        viewBtn: true,
        copyBtn: true,
        column: {
          avatar: {
            label: "头像",
            type: "upload",
            listType: "picture-img",
            action: "/upload/file",
            propsHttp: {
              res: "data",
              url: "fileUrl",
            },
          },
          username: {
            label: "账号",
            span: 24,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入账号",
                trigger: "blur",
              },
            ],
          },

          name: {
            span: 24,
            label: "真实名称",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入真实名称",
                trigger: "blur",
              },
            ],
          },
          phone: {
            span: 24,
            label: "手机号",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入手机号",
                trigger: "blur",
              },
            ],
          },
          sex: {
            span: 24,
            label: "性别",
            type: "radio",
            dicData: [
              {label: "男", value: 1},
              {label: "女", value: 2},
              {label: "未知", value: 0},
            ],
          },
          deptId: {
            span: 24,
            label: "部门",
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/admin/sysDept/treeList`,
            rules: [
              {
                required: true,
                message: "请选择部门",
                trigger: "blur",
              },
            ],
          },
          roleId: {
            span: 24,
            label: "角色",
            type: "select",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/admin/sysRole/list`,
            rules: [
              {
                required: true,
                message: "请选择角色",
                trigger: "blur",
              },
            ],
          },
          jobId: {
            span: 24,
            label: "岗位",
            type: "select",
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/admin/sysJob/list`,
            rules: [
              {
                required: true,
                message: "请选择岗位",
                trigger: "blur",
              },
            ],
          },
        },
      },
    };
  },
  created() {
    treeList().then((res) => {
      this.treeData = res.data.data;
    });
  },
  computed: {
    ...mapGetters(["permission"]),

    selectIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
  },

  methods: {
    download() {
      this.$download("/admin/sysUser/download", "GET", {id: 1});
    },
    deptClick(val) {
      if (this.selectDeptId === val.id) {
        this.$refs.tree.setCurrentKey(null);
        this.selectDeptId = null;
      } else {
        this.$refs.tree.setChecked(val, true);
        this.selectDeptId = val.id;
      }
      this.params = Object.assign(this.params, {deptId: this.selectDeptId});
      this.init();
    },
    init() {
      this.option.loading = true;
      pageList(this.page, this.params)
          .then((res) => {
            this.data = res.data.data.records;
            this.page.total = res.data.data.total;
            this.option.loading = false;
          })
          .catch(() => {
            this.option.loading = false;
          });
    },
    save(row, done, loading) {
      loading();
      save(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    update(row, index, done, loading) {
      loading();
      update(row).then((res) => {
        this.refreshLoad(res);
      });
      done();
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        type: "warning",
      })
          .then(() => {
            return remove(row.id);
          })
          .then((res) => {
            this.refreshLoad(res);
          });
    },
    resetPassword() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择用户密码重置?  默认密码：123456", {
        type: "warning",
      })
          .then(() => {
            return resetPassword(this.selectIds);
          })
          .then(() => {
            this.refreshLoad();
          });
    },
    delBatch() {
      if (this.selectIds.length === 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        type: "warning",
      })
          .then(() => {
            return remove(this.selectIds);
          })
          .then(() => {
            this.refreshLoad();
          });
    },
    refreshLoad(res) {
      this.init();
      this.$message({
        type: "success",
        message: res.data.msg,
      });
    },
    search(form, done) {
      this.params = form;
      this.init();
      done();
    },
    permissionList() {
      return {
        addBtn: !this.validatenull(this.permission.SysUser_add),
        delBtn: !this.validatenull(this.permission.SysUser_remove),
        editBtn: !this.validatenull(this.permission.SysUser_edit),
        viewBtn: !this.validatenull(this.permission.SysUser_view),
        copyBtn: !this.validatenull(this.permission.SysUser_copy),
      };
    },
    refresh() {
      this.init();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
